package com.eleadmin.store.biz.impl;

import com.eleadmin.damon.utils.constant.SqlConstant;
import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.store.result.SelectResult;
import com.eleadmin.store.biz.ProductBiz;
import com.eleadmin.store.entity.Product;
import com.eleadmin.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2024-02-13 18:39:57
 */
@Repository
public class ProductBizImpl extends BaseDao implements ProductBiz {

    @Override
    public JsonResult list(PageResult2<T> page, Product product, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Product> rm = new BeanPropertyRowMapper<Product>(Product.class);
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.name");
        sql.append(",p.lengths,p.negative");
        sql.append(",p.code,p.specification,p.liWeight,p.remark");
        sql.append(",p.orderNum,p.create_time,p.create_user,p.update_time");
        sql.append(",p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from we_product p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=p.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
        sql.append(" where p.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(product)) {
            if (Utils.isNotEmpty(product.getName())) {
                sql.append(" and instr(p.name,:name)>0");
                map.put("name", product.getName());
            }
            if (Utils.isNotEmpty(product.getKeywords())) {
                sql.append(" and instr(p.code,:code)>0 ");
                map.put("code", product.getKeywords());
            }
            if (Utils.isNotEmpty(product.getSpecification())) {
                sql.append(" and instr(p.specification,:specification)>0 ");
                map.put("specification", product.getSpecification());
            }
            if (Utils.isNotEmpty(product.getSort()) && Utils.isNotEmpty(product.getOrder())) {
                sql.append(" order by p." + product.getSort() + " " + product.getOrder());
            } else {
                sql.append(" order by p.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<Product> list(Product product, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Product> rm = new BeanPropertyRowMapper<Product>(Product.class);
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.name");
        sql.append(",p.lengths,p.negative");
        sql.append(",p.code,p.specification,p.liWeight,p.remark");
        sql.append(",p.orderNum,p.create_time,p.create_user,p.update_time");
        sql.append(",p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from we_product p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
        sql.append(" where p.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(product)) {
            if (Utils.isNotEmpty(product.getName())) {
                sql.append(" and instr(p.name,:name)>0");
                map.put("name", product.getName());
            }
            if (Utils.isNotEmpty(product.getCode())) {
                sql.append(" and instr(p.code,:code)>0 ");
                map.put("code", product.getCode());
            }
            if (Utils.isNotEmpty(product.getSpecification())) {
                sql.append(" and instr(p.specification,:specification)>0 ");
                map.put("specification", product.getSpecification());
            }
            if (Utils.isNotEmpty(product.getSort()) && Utils.isNotEmpty(product.getOrder())) {
                sql.append(" order by p." + product.getSort() + " " + product.getOrder());
            } else {
                sql.append(" order by p.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public List<SelectResult> listSel(Product product, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<SelectResult> rm = new BeanPropertyRowMapper<SelectResult>(SelectResult.class);
        StringBuffer sql = new StringBuffer("select p.id value");
        sql.append(",CONCAT(p.name,'-',p.specification,IFNULL(CONCAT('-',p.lengths),''),IFNULL(CONCAT('-',p.negative),'')) label");
        sql.append(",p.name,p.lengths,p.negative");
        sql.append(",p.code,p.specification,p.liWeight,p.remark");
        sql.append(" from we_product p ");
        sql.append(" where p.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(product)) {
            if (Utils.isNotEmpty(product.getName())) {
                sql.append(" and (instr(p.name,:name)>0 or instr(p.code,:name)>0) ");
                map.put("name", product.getName());
            }
            if (Utils.isNotEmpty(product.getSort()) && Utils.isNotEmpty(product.getOrder())) {
                sql.append(" order by p." + product.getSort() + " " + product.getOrder());
            } else {
                sql.append(" order by p.create_time desc");
            }
        }
        sql.append(" limit "+ SqlConstant.LIMIT);
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public List<SelectResult> listSelUnique(Product product, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<SelectResult> rm = new BeanPropertyRowMapper<SelectResult>(SelectResult.class);
        StringBuffer sql = new StringBuffer("select ROW_NUMBER() OVER (ORDER BY pp.name) value,pp.label,pp.name from ( ");
        sql.append(" select distinct ");
        sql.append("p.name label");
        sql.append(",p.name");
        sql.append(" from we_product p ");
        sql.append(" where p.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(product)) {
            if (Utils.isNotEmpty(product.getName())) {
                sql.append(" and (instr(p.name,:name)>0 or instr(p.code,:name)>0) ");
                map.put("name", product.getName());
            }
        }
        sql.append(" limit "+ SqlConstant.LIMIT);
        sql.append(" ) pp");
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public Product getById(String id) {
        RowMapper<Product> rm = new BeanPropertyRowMapper<Product>(Product.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.name");
        sql.append(",p.lengths,p.negative");
        sql.append(",p.code,p.specification,p.liWeight,p.remark");
        sql.append(",p.orderNum,p.create_time,p.create_user,p.update_time");
        sql.append(",p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from we_product p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
        sql.append(" where p.deleted=0 and p.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        try {
            return jdbcTemplate.queryForObject(sql.toString(), map, rm);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean save(Product product, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into we_product (");
        sql.append("organization_id,name,code,specification");
        sql.append(",lengths,negative");
        sql.append(",liWeight,remark,orderNum,create_user,create_time) values ( ");
        sql.append(":organization_id,:name,:code,:specification");
        sql.append(",:lengths,:negative");
        sql.append(",:liWeight,:remark,:orderNum,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("name", product.getName());
        map.put("code", product.getCode());
        map.put("specification", product.getSpecification());
        map.put("lengths", product.getLengths());
        map.put("negative", product.getNegative());
        map.put("liWeight", product.getLiweight());
        map.put("remark", product.getRemark());
        map.put("orderNum", product.getOrdernum() == null ? null : product.getOrdernum() + "");
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(Product product, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update we_product set ");
        sql.append("name=:name,code=:code,specification=:specification ");
        sql.append(",lengths=:lengths,negative=:negative ");
        sql.append(",liWeight=:liWeight,remark=:remark,orderNum=:orderNum ");
        sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("name", product.getName());
        map.put("code", product.getCode());
        map.put("specification", product.getSpecification());
        map.put("lengths", product.getLengths());
        map.put("negative", product.getNegative());
        map.put("liWeight", product.getLiweight());
        map.put("remark", product.getRemark());
        map.put("orderNum", product.getOrdernum() == null ? null : product.getOrdernum() + "");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", product.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update we_product set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
